package cn.bouncyslime.graduationProject.nba.crawler.spider;

import cn.bouncyslime.graduationProject.nba.dto.CompetitionDto;
import cn.bouncyslime.graduationProject.nba.dto.PlayerCompetitionDataDto;
import cn.bouncyslime.graduationProject.nba.util.BigDecimalUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.ConsolePipeline;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 张一卓
 * @date 2020/5/6 17:55
 **/
public class CompetitionPageProcessor implements PageProcessor {

    private static final Logger logger = LoggerFactory.getLogger(CompetitionPageProcessor.class);

    private static final String BASE_URL = "http://www.stat-nba.com/game/";

    private static String seasonRegex = " (.*)赛季 (.*) ";

    private static Pattern seasonPattern = Pattern.compile(seasonRegex);

    private static String competitionIdRegex = ".*/(.*)\\.html";

    private static Pattern competitionPattern = Pattern.compile(competitionIdRegex);

    private static String teamIdRegex = "/([a-zA-Z]+)/([a-zA-Z]+).([a-zA-Z]+)";

    private static Pattern teamIdPattern = Pattern.compile(teamIdRegex);

    private static String playerIdRegex = "/([a-zA-Z]+)/(\\d+).([a-zA-Z]+)";

    private static Pattern playerIdPattern = Pattern.compile(playerIdRegex);

    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    private Site site = Site.me().setSleepTime(1000).setRetryTimes(5).setRetrySleepTime(2000).setCharset("utf-8")
            .setUserAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.369")
            .addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
            .addHeader("Connection", "keep-alive")
            .addHeader("Host", "www.stat-nba.com")
            .addCookie("__cfduid", "def62db9852e6b626f3d744bacadd235a1585654597")
            .addCookie("Hm_lvt_102e5c22af038a553a8610096bcc8bd4", "1585654597")
            .addCookie("yjs_id", "aHR0cHM6Ly93d3cuYmFpZHUuY29tL2xpbms/dXJsPXlGVU92Y0NMMERvR3ZUT01SVm1kS05NeExSeDNEWHFxcklfakxDSXVpMHJxY1E0RkJITTFncEhGX2lQcHJ2ekgmd2Q9JmVxaWQ9ZGM3OTgyN2YwMDczMjg1OTAwMDAwMDAzNWU4MzJiNDN8MTU4NTY1NDU5Njg0Ng")
            .addCookie("ctrl_time", "1")
            .addCookie("Hm_lpvt_102e5c22af038a553a8610096bcc8bd4", "1585672286");

    @Override
    public void process(Page page) {
        String pageUrl = page.getUrl().toString();
        if (page.isDownloadSuccess()) {
            Html html = page.getHtml();
            String bodyText = html.xpath("body/text()").get();
            if (!bodyText.contains("select * from")) {
                logger.info("开始爬取页面");
                CompetitionDto competitionDto = new CompetitionDto();
                Integer competitionId = 0;
                Matcher matcher = competitionPattern.matcher(pageUrl);
                if (matcher.find()) {
                    competitionId = Integer.valueOf(matcher.group(1));
                    competitionDto.setCompetitionId(competitionId);
                }
                // 构造下一条比赛数据
                page.addTargetRequest(BASE_URL + (competitionId - 1) + ".html");
                // 以下为获取赛季信息，包括赛季和比赛类型（常规赛、季后赛）
                String seasonString = html.xpath("div[@class='title']/text()").get();
//                logger.debug(seasonString);
                matcher = seasonPattern.matcher(seasonString);
                if (matcher.find()) {
                    String season = matcher.group(1);
                    String competitionType = matcher.group(2);
                    if ("常规赛".equals(competitionType)) {
                        competitionDto.setCompetitionType("regular");
                    } else if ("季后赛".equals(competitionType)) {
                        competitionDto.setCompetitionType("post");
                    }
                    int seasonPrefix = Integer.parseInt(season.substring(0, 2));
                    if (seasonPrefix < 45) {
                        season = "20" + seasonPrefix;
                    } else {
                        season = "19" + seasonPrefix;
                    }
//                    logger.debug(season);
                    competitionDto.setSeason(season);
                }

                // 以下为获取比赛日期
                // div[@class='title']/following::*[1]/text()
                // div[@class='title']/following::div[2]/text()
                String competitionDate = html.xpath("div[@style='float: left;margin-top: 25px;margin-left: 10px;font-size: 16px;font-weight: bold;color: #009CFF']/text()").get();
//                logger.debug(competitionDate);
                try {
                    competitionDto.setCompetitionDate(dateFormat.parse(competitionDate));
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                // 以下为获取比赛两个球队的ID
                List<String> teamIdList = html.xpath("div[@class='basic']/div[@class='team']/div[@class='teamDiv']/a/@href").all();
//                logger.debug(teamIdList.toString());
                String homeTeamId = null;
                String visitingTeamId = null;
                matcher = teamIdPattern.matcher(teamIdList.get(0));
                if (matcher.find()) {
                    visitingTeamId = matcher.group(2);
                    competitionDto.setVisitingFieldTeamId(visitingTeamId);
                }
                matcher = teamIdPattern.matcher(teamIdList.get(1));
                if (matcher.find()) {
                    homeTeamId = matcher.group(2);
                    competitionDto.setHomeFieldTeamId(homeTeamId);
                }

                // 以下为获取比赛比分
                List<String> scoreList = html.xpath("div[@class='scorebox']/div[@class='text']/div[@class='score']/text()").all();
                Integer visitingScore = Integer.valueOf(scoreList.get(0));
                Integer homeScore = Integer.valueOf(scoreList.get(1));
                competitionDto.setVisitingFieldScore(visitingScore);
                competitionDto.setHomeFieldScore(homeScore);
                if (homeScore > visitingScore) {
                    competitionDto.setWinner(homeTeamId);
                    competitionDto.setLoser(visitingTeamId);
                } else {
                    competitionDto.setWinner(visitingTeamId);
                    competitionDto.setLoser(homeTeamId);
                }
                // 以下为获取比赛各节的比分
                List<Selectable> periodSelectalbeList = html.xpath("div[@class='scorebox']/div[@class='table']").nodes();
                List<String> visitingFieldAllPeriodScoreList = periodSelectalbeList.get(0).xpath("table/tbody/tr/td[2]/text()").all();
                competitionDto.setVisitingFieldFirstPeriodScore(Integer.valueOf(visitingFieldAllPeriodScoreList.get(0)));
                competitionDto.setVisitingFieldSecondPeriodScore(Integer.valueOf(visitingFieldAllPeriodScoreList.get(1)));
                competitionDto.setVisitingFieldThirdPeriodScore(Integer.valueOf(visitingFieldAllPeriodScoreList.get(2)));
                competitionDto.setVisitingFieldFourthPeriodScore(Integer.valueOf(visitingFieldAllPeriodScoreList.get(3)));
                List<String> homeFieldAllPeriodScoreList = periodSelectalbeList.get(1).xpath("table/tbody/tr/td[1]/text()").all();
                competitionDto.setHomeFieldFirstPeriodScore(Integer.valueOf(homeFieldAllPeriodScoreList.get(0)));
                competitionDto.setHomeFieldSecondPeriodScore(Integer.valueOf(homeFieldAllPeriodScoreList.get(1)));
                competitionDto.setHomeFieldThirdPeriodScore(Integer.valueOf(homeFieldAllPeriodScoreList.get(2)));
                competitionDto.setHomeFieldFourthPeriodScore(Integer.valueOf(homeFieldAllPeriodScoreList.get(3)));
//                logger.debug(visitingFieldAllPeriodScoreList.toString());
//                logger.debug(homeFieldAllPeriodScoreList.toString());

                // 以下为获取球队比赛中的表现
                List<Selectable> competitionDataSelectableList = html.xpath("div[@class='detail']/div[@class='stat_box']").nodes();
                // 客场球队表现
                List<String> visitingTeamDataList = competitionDataSelectableList.get(0).xpath("//table[@class='stat_box']/tbody/tr[@class='team_all_content']/td/text()").all();
                String visitingTeamData = null;
                for (int i=1; i<visitingTeamDataList.size(); i++){
                    visitingTeamData = visitingTeamDataList.get(i);
                    if (visitingTeamData != null && !"".equals(visitingTeamData.trim())) {
                        switch (i) {
                            case 1:
                                competitionDto.setVisitingFieldAppearances(Integer.valueOf(visitingTeamData.replace("人", "")));
                                break;
                            case 2:
                                competitionDto.setVisitingFieldShootingPercentage(BigDecimalUtil.div(visitingTeamData.replace("%", ""), "100", 3));
                                break;
                            case 3:
                                competitionDto.setVisitingFieldShootingAmount(Integer.valueOf(visitingTeamData));
                                break;
                            case 4:
                                competitionDto.setVisitingFieldShootingAttempts(Integer.valueOf(visitingTeamData));
                                break;
                            case 5:
                                competitionDto.setVisitingFieldThreePointPercentage(BigDecimalUtil.div(visitingTeamData.replace("%", ""), "100", 3));
                                break;
                            case 6:
                                competitionDto.setVisitingFieldThreePointAmount(Integer.valueOf(visitingTeamData));
                                break;
                            case 7:
                                competitionDto.setVisitingFieldThreePointAttempts(Integer.valueOf(visitingTeamData));
                                break;
                            case 8:
                                competitionDto.setVisitingFieldPenaltyKickPercentage(BigDecimalUtil.div(visitingTeamData.replace("%", ""), "100", 3));
                                break;
                            case 9:
                                competitionDto.setVisitingFieldPenaltyKickAmount(Integer.valueOf(visitingTeamData));
                                break;
                            case 10:
                                competitionDto.setVisitingFieldPenaltyKickAttempts(Integer.valueOf(visitingTeamData));
                                break;
                            case 11:
                                competitionDto.setVisitingFieldTrueShootingPercentage(BigDecimalUtil.div(visitingTeamData.replace("%", ""), "100", 3));
                                break;
                            case 12:
                                competitionDto.setVisitingFieldRebound(Integer.valueOf(visitingTeamData));
                                break;
                            case 13:
                                competitionDto.setVisitingFieldFrontRebound(Integer.valueOf(visitingTeamData));
                                break;
                            case 14:
                                competitionDto.setVisitingFieldBackRebound(Integer.valueOf(visitingTeamData));
                                break;
                            case 15:
                                competitionDto.setVisitingFieldAssists(Integer.valueOf(visitingTeamData));
                                break;
                            case 16:
                                competitionDto.setVisitingFieldSteal(Integer.valueOf(visitingTeamData));
                                break;
                            case 17:
                                competitionDto.setVisitingFieldBlockShot(Integer.valueOf(visitingTeamData));
                                break;
                            case 18:
                                competitionDto.setVisitingFieldFault(Integer.valueOf(visitingTeamData));
                                break;
                            case 19:
                                competitionDto.setVisitingFieldFoul(Integer.valueOf(visitingTeamData));
                                break;
                        }
                    }
                }

                // 主场球队表现
                List<String> homeTeamDataList = competitionDataSelectableList.get(1).xpath("//table[@class='stat_box']/tbody/tr[@class='team_all_content']/td/text()").all();
                String homeTeamData = null;
                for (int i=1; i<homeTeamDataList.size(); i++) {
                    homeTeamData = homeTeamDataList.get(i);
                    if (homeTeamData != null && !"".equals(homeTeamData.trim())) {
                        switch (i) {
                            case 1:
                                competitionDto.setHomeFieldAppearances(Integer.valueOf(homeTeamData.replace("人", "")));
                                break;
                            case 2:
                                competitionDto.setHomeFieldShootingPercentage(BigDecimalUtil.div(homeTeamData.replace("%", ""), "100", 3));
                                break;
                            case 3:
                                competitionDto.setHomeFieldShootingAmount(Integer.valueOf(homeTeamData));
                                break;
                            case 4:
                                competitionDto.setHomeFieldShootingAttempts(Integer.valueOf(homeTeamData));
                                break;
                            case 5:
                                competitionDto.setHomeFieldThreePointPercentage(BigDecimalUtil.div(homeTeamData.replace("%", ""), "100", 3));
                                break;
                            case 6:
                                competitionDto.setHomeFieldThreePointAmount(Integer.valueOf(homeTeamData));
                                break;
                            case 7:
                                competitionDto.setHomeFieldThreePointAttempts(Integer.valueOf(homeTeamData));
                                break;
                            case 8:
                                competitionDto.setHomeFieldPenaltyKickPercentage(BigDecimalUtil.div(homeTeamData.replace("%", ""), "100", 3));
                                break;
                            case 9:
                                competitionDto.setHomeFieldPenaltyKickAmount(Integer.valueOf(homeTeamData));
                                break;
                            case 10:
                                competitionDto.setHomeFieldPenaltyKickAttempts(Integer.valueOf(homeTeamData));
                                break;
                            case 11:
                                competitionDto.setHomeFieldTrueShootingPercentage(BigDecimalUtil.div(homeTeamData.replace("%", ""), "100", 3));
                                break;
                            case 12:
                                competitionDto.setHomeFieldRebound(Integer.valueOf(homeTeamData));
                                break;
                            case 13:
                                competitionDto.setHomeFieldFrontRebound(Integer.valueOf(homeTeamData));
                                break;
                            case 14:
                                competitionDto.setHomeFieldBackRebound(Integer.valueOf(homeTeamData));
                                break;
                            case 15:
                                competitionDto.setHomeFieldAssists(Integer.valueOf(homeTeamData));
                                break;
                            case 16:
                                competitionDto.setHomeFieldSteal(Integer.valueOf(homeTeamData));
                                break;
                            case 17:
                                competitionDto.setHomeFieldBlockShot(Integer.valueOf(homeTeamData));
                                break;
                            case 18:
                                competitionDto.setHomeFieldFault(Integer.valueOf(homeTeamData));
                                break;
                            case 19:
                                competitionDto.setHomeFieldFoul(Integer.valueOf(homeTeamData));
                                break;
                        }
                    }
                }   
                    
//                competitionDto.setHomeFieldAppearances(Integer.valueOf(homeTeamData.get(1).replace("人", "")));
//                competitionDto.setHomeFieldShootingPercentage(BigDecimalUtil.div(homeTeamData.get(2).replace("%", ""), "100", 3));
//                competitionDto.setHomeFieldShootingAmount(Integer.valueOf(homeTeamData.get(3)));
//                competitionDto.setHomeFieldShootingAttempts(Integer.valueOf(homeTeamData.get(4)));
//                competitionDto.setHomeFieldThreePointPercentage(BigDecimalUtil.div(homeTeamData.get(5).replace("%", ""), "100", 3));
//                competitionDto.setHomeFieldThreePointAmount(Integer.valueOf(homeTeamData.get(6)));
//                competitionDto.setHomeFieldThreePointAttempts(Integer.valueOf(homeTeamData.get(7)));
//                competitionDto.setHomeFieldPenaltyKickPercentage(BigDecimalUtil.div(homeTeamData.get(8).replace("%", ""), "100", 3));
//                competitionDto.setHomeFieldPenaltyKickAmount(Integer.valueOf(homeTeamData.get(9)));
//                competitionDto.setHomeFieldPenaltyKickAttempts(Integer.valueOf(homeTeamData.get(10)));
//                competitionDto.setHomeFieldTrueShootingPercentage(BigDecimalUtil.div(homeTeamData.get(11).replace("%", ""), "100", 3));
//                competitionDto.setHomeFieldRebound(Integer.valueOf(homeTeamData.get(12)));
//                competitionDto.setHomeFieldFrontRebound(Integer.valueOf(homeTeamData.get(13)));
//                competitionDto.setHomeFieldBackRebound(Integer.valueOf(homeTeamData.get(14)));
//                competitionDto.setHomeFieldAssists(Integer.valueOf(homeTeamData.get(15)));
//                competitionDto.setHomeFieldSteal(Integer.valueOf(homeTeamData.get(16)));
//                competitionDto.setHomeFieldBlockShot(Integer.valueOf(homeTeamData.get(17)));
//                competitionDto.setHomeFieldFault(Integer.valueOf(homeTeamData.get(18)));
//                competitionDto.setHomeFieldFoul(Integer.valueOf(homeTeamData.get(19)));

                // 爬取球员比赛数据
                List<PlayerCompetitionDataDto> playerCompetitionDataDtoList = new ArrayList<>(competitionDto.getHomeFieldAppearances() + competitionDto.getVisitingFieldAppearances());
                List<Selectable> visitingPlayerSelectable = competitionDataSelectableList.get(0).xpath("//table[@class='stat_box']/tbody/tr[@class='sort']").nodes();
                for (Selectable selectable : visitingPlayerSelectable) {
                    PlayerCompetitionDataDto playerCompetitionDataDto = new PlayerCompetitionDataDto();
                    playerCompetitionDataDto.setCompetitionId(competitionId);
                    playerCompetitionDataDto.setTeamId(visitingTeamId);
                    matcher = playerIdPattern.matcher(selectable.xpath("td/a/@href").get());
                    if (matcher.find()) {
                        playerCompetitionDataDto.setPlayerId(Integer.valueOf(matcher.group(2)));
                    }
                    List<String> playerDataList = selectable.xpath("td/text()").all();
                    String cell = null;
                    for (int i = 0; i < playerDataList.size(); i++) {
                        cell = playerDataList.get(i);
                        if (cell != null && !"".equals(cell.trim())) {
                            switch (i) {
                                case 2:
                                    playerCompetitionDataDto.setIsStartingIn(Integer.valueOf(cell));
                                    break;
                                case 3:
                                    playerCompetitionDataDto.setPlayingTime(Integer.valueOf(cell));
                                    break;
                                case 4:
                                    playerCompetitionDataDto.setShootingPercentage(BigDecimalUtil.div(cell.replace("%", ""), "100", 3));
                                    break;
                                case 5:
                                    playerCompetitionDataDto.setShootingAmount(Integer.valueOf(cell));
                                    break;
                                case 6:
                                    playerCompetitionDataDto.setShootingAttempts(Integer.valueOf(cell));
                                    break;
                                case 7:
                                    playerCompetitionDataDto.setThreePointPercentage(BigDecimalUtil.div(cell.replace("%", ""), "100", 3));
                                    break;
                                case 8:
                                    playerCompetitionDataDto.setThreePointAmount(Integer.valueOf(cell));
                                    break;
                                case 9:
                                    playerCompetitionDataDto.setThreePointAttempts(Integer.valueOf(cell));
                                    break;
                                case 10:
                                    playerCompetitionDataDto.setPenaltyKickPercentage(BigDecimalUtil.div(cell.replace("%", ""), "100", 3));
                                    break;
                                case 11:
                                    playerCompetitionDataDto.setPenaltyKickAmount(Integer.valueOf(cell));
                                    break;
                                case 12:
                                    playerCompetitionDataDto.setPenaltyKickAttempts(Integer.valueOf(cell));
                                    break;
                                case 13:
                                    playerCompetitionDataDto.setTrueShootingPercentage(BigDecimalUtil.div(cell.replace("%", ""), "100", 3));
                                    break;
                                case 14:
                                    playerCompetitionDataDto.setRebound(Integer.valueOf(cell));
                                    break;
                                case 15:
                                    playerCompetitionDataDto.setFrontRebound(Integer.valueOf(cell));
                                    break;
                                case 16:
                                    playerCompetitionDataDto.setBackRebound(Integer.valueOf(cell));
                                    break;
                                case 17:
                                    playerCompetitionDataDto.setAssists(Integer.valueOf(cell));
                                    break;
                                case 18:
                                    playerCompetitionDataDto.setSteal(Integer.valueOf(cell));
                                    break;
                                case 19:
                                    playerCompetitionDataDto.setBlockShot(Integer.valueOf(cell));
                                    break;
                                case 20:
                                    playerCompetitionDataDto.setFault(Integer.valueOf(cell));
                                    break;
                                case 21:
                                    playerCompetitionDataDto.setFoul(Integer.valueOf(cell));
                                    break;
                                case 22:
                                    playerCompetitionDataDto.setScore(Integer.valueOf(cell));
                                    break;
                            }
                        }
                    }
//                    logger.debug(playerCompetitionDataDto.toString());
                    playerCompetitionDataDtoList.add(playerCompetitionDataDto);
//                    logger.debug(selectable.xpath("td/a/@href").get());
//                    logger.debug(selectable.xpath("td/text()").all().toString());
                }

                List<Selectable> homePlayerSelectable = competitionDataSelectableList.get(1).xpath("//table[@class='stat_box']/tbody/tr[@class='sort']").nodes();
                for (Selectable selectable : homePlayerSelectable) {
                    PlayerCompetitionDataDto playerCompetitionDataDto = new PlayerCompetitionDataDto();
                    playerCompetitionDataDto.setCompetitionId(competitionId);
                    playerCompetitionDataDto.setTeamId(visitingTeamId);
                    matcher = playerIdPattern.matcher(selectable.xpath("td/a/@href").get());
                    if (matcher.find()) {
                        playerCompetitionDataDto.setPlayerId(Integer.valueOf(matcher.group(2)));
                    }
                    List<String> playerDataList = selectable.xpath("td/text()").all();
                    String cell = null;
                    for (int i = 0; i < playerDataList.size(); i++) {
                        cell = playerDataList.get(i);
                        if (cell != null && !"".equals(cell.trim())) {
                            switch (i) {
                                case 2:
                                    playerCompetitionDataDto.setIsStartingIn(Integer.valueOf(cell));
                                    break;
                                case 3:
                                    playerCompetitionDataDto.setPlayingTime(Integer.valueOf(cell));
                                    break;
                                case 4:
                                    playerCompetitionDataDto.setShootingPercentage(BigDecimalUtil.div(cell.replace("%", ""), "100", 3));
                                    break;
                                case 5:
                                    playerCompetitionDataDto.setShootingAmount(Integer.valueOf(cell));
                                    break;
                                case 6:
                                    playerCompetitionDataDto.setShootingAttempts(Integer.valueOf(cell));
                                    break;
                                case 7:
                                    playerCompetitionDataDto.setThreePointPercentage(BigDecimalUtil.div(cell.replace("%", ""), "100", 3));
                                    break;
                                case 8:
                                    playerCompetitionDataDto.setThreePointAmount(Integer.valueOf(cell));
                                    break;
                                case 9:
                                    playerCompetitionDataDto.setThreePointAttempts(Integer.valueOf(cell));
                                    break;
                                case 10:
                                    playerCompetitionDataDto.setPenaltyKickPercentage(BigDecimalUtil.div(cell.replace("%", ""), "100", 3));
                                    break;
                                case 11:
                                    playerCompetitionDataDto.setPenaltyKickAmount(Integer.valueOf(cell));
                                    break;
                                case 12:
                                    playerCompetitionDataDto.setPenaltyKickAttempts(Integer.valueOf(cell));
                                    break;
                                case 13:
                                    playerCompetitionDataDto.setTrueShootingPercentage(BigDecimalUtil.div(cell.replace("%", ""), "100", 3));
                                    break;
                                case 14:
                                    playerCompetitionDataDto.setRebound(Integer.valueOf(cell));
                                    break;
                                case 15:
                                    playerCompetitionDataDto.setFrontRebound(Integer.valueOf(cell));
                                    break;
                                case 16:
                                    playerCompetitionDataDto.setBackRebound(Integer.valueOf(cell));
                                    break;
                                case 17:
                                    playerCompetitionDataDto.setAssists(Integer.valueOf(cell));
                                    break;
                                case 18:
                                    playerCompetitionDataDto.setSteal(Integer.valueOf(cell));
                                    break;
                                case 19:
                                    playerCompetitionDataDto.setBlockShot(Integer.valueOf(cell));
                                    break;
                                case 20:
                                    playerCompetitionDataDto.setFault(Integer.valueOf(cell));
                                    break;
                                case 21:
                                    playerCompetitionDataDto.setFoul(Integer.valueOf(cell));
                                    break;
                                case 22:
                                    playerCompetitionDataDto.setScore(Integer.valueOf(cell));
                                    break;
                            }
                        }
                    }
//                    playerCompetitionDataDto.setIsStartingIn(Integer.valueOf(playerDataList.get(2)));
//                    playerCompetitionDataDto.setPlayingTime(Integer.valueOf(playerDataList.get(3)));
//                    playerCompetitionDataDto.setShootingPercentage(BigDecimalUtil.div(playerDataList.get(4).replace("%", ""), "100", 3));
//                    playerCompetitionDataDto.setShootingAmount(Integer.valueOf(playerDataList.get(5)));
//                    playerCompetitionDataDto.setShootingAttempts(Integer.valueOf(playerDataList.get(6)));
//                    playerCompetitionDataDto.setThreePointPercentage(BigDecimalUtil.div(playerDataList.get(7).replace("%", ""), "100", 3));
//                    playerCompetitionDataDto.setThreePointAmount(Integer.valueOf(playerDataList.get(8)));
//                    playerCompetitionDataDto.setThreePointAttempts(Integer.valueOf(playerDataList.get(9)));
//                    playerCompetitionDataDto.setPenaltyKickPercentage(BigDecimalUtil.div(playerDataList.get(10).replace("%", ""), "100", 3));
//                    playerCompetitionDataDto.setPenaltyKickAmount(Integer.valueOf(playerDataList.get(11)));
//                    playerCompetitionDataDto.setPenaltyKickAttempts(Integer.valueOf(playerDataList.get(12)));
//                    playerCompetitionDataDto.setTrueShootingPercentage(BigDecimalUtil.div(playerDataList.get(13).replace("%", ""), "100", 3));
//                    playerCompetitionDataDto.setRebound(Integer.valueOf(playerDataList.get(14)));
//                    playerCompetitionDataDto.setFrontRebound(Integer.valueOf(playerDataList.get(15)));
//                    playerCompetitionDataDto.setBackRebound(Integer.valueOf(playerDataList.get(16)));
//                    playerCompetitionDataDto.setAssists(Integer.valueOf(playerDataList.get(17)));
//                    playerCompetitionDataDto.setSteal(Integer.valueOf(playerDataList.get(18)));
//                    playerCompetitionDataDto.setBlockShot(Integer.valueOf(playerDataList.get(19)));
//                    playerCompetitionDataDto.setFault(Integer.valueOf(playerDataList.get(20)));
//                    playerCompetitionDataDto.setFoul(Integer.valueOf(playerDataList.get(21)));
//                    playerCompetitionDataDto.setScore(Integer.valueOf(playerDataList.get(22)));
//                    logger.debug(playerCompetitionDataDto.toString());
                    playerCompetitionDataDtoList.add(playerCompetitionDataDto);
//                    logger.debug(selectable.xpath("td/a/@href").get());
//                    logger.debug(selectable.xpath("td/text()").all().toString());
                }
//                logger.debug(competitionDto.toString());
                page.putField("competition", competitionDto);
                page.putField("playerCompetitionDataList", playerCompetitionDataDtoList);
                logger.info("页面爬取成功");
            } else {
                logger.info("没有该页面，url：" + pageUrl);
                Integer competitionId = 0;
                Matcher matcher = competitionPattern.matcher(pageUrl);
                if (matcher.find()) {
                    competitionId = Integer.valueOf(matcher.group(1));
                }
                if (competitionId <= 45444){
                    page.addTargetRequest(BASE_URL + (competitionId - 1) + ".html");
                }
            }
        }
    }

    @Override
    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
        Spider.create(new CompetitionPageProcessor())
                .addUrl("http://www.stat-nba.com/game/39013.html")
                .addPipeline(new ConsolePipeline())
                .thread(5).run();
    }
}
